package annuaire.rmi.remotedObject.impl;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Set;

import annuaire.beans.Admin;
import annuaire.beans.Promotion;
import annuaire.beans.Student;
import annuaire.exceptions.BusinessException;
import annuaire.exceptions.DaoException;
import annuaire.rmi.remotedObject.IAdminService;

/**
 * Classe d'objet distant RMI, servant à gérer les services administrateur.
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class AdminImpl extends StudentImpl implements IAdminService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @throws RemoteException
	 */
	public AdminImpl() throws RemoteException {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IAdminService#addPromo(beans.Promotion)
	 */
	@Override
	public Promotion addPromo(Promotion p) throws Exception {

		try {
			if (p == null) {
				return null;
			}

			p = dao.addPromo(p);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new BusinessException(e);
		}

		return p;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IAdminService#addStudent(beans.Student)
	 */
	@Override
	public Student addStudent(Student s) throws Exception {
		try {
			if (s == null) {
				return null;
			}

			s = dao.addStudent(s);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new BusinessException(e);
		}

		return s;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IAdminService#addStudentsToPromo(beans.Promotion,
	 * java.util.Set)
	 */
	@Override
	public boolean addStudentsToPromo(Promotion p, Set<Student> setStudents)
			throws Exception {

		if (p == null || setStudents == null) {
			return false;
		}

		dao.addStudentsToPromo(p, setStudents);

		return true;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IAdminService#deletePromo(beans.Promotion)
	 */
	@Override
	public boolean deletePromo(Promotion p) throws Exception {

		try {
			if (p == null) {
				return false;
			}
			dao.removePromo(p);
			return true;
		} catch (DaoException e) {
			e.printStackTrace();
			throw new BusinessException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IAdminService#deleteStudent(beans.Student)
	 */
	@Override
	public boolean deleteStudent(Student s) throws Exception {
		try {
			if (s == null) {
				return false;
			}
			dao.removeStudent(s);
			return true;
		} catch (DaoException e) {
			e.printStackTrace();
			throw new BusinessException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IAdminService#updatePromo(beans.Promotion)
	 */
	@Override
	public Promotion updatePromo(Promotion p) throws Exception {

		try {
			if (p == null) {
				return null;
			}

			p = dao.updatePromo(p);
			return p;
		} catch (DaoException e) {
			e.printStackTrace();
			throw new BusinessException(e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * annuaire.rmi.remotedObject.impl.StudentImpl#updateBeans(java.io.Serializable
	 * )
	 */
	@Override
	public void updateBeans(Serializable newBeans) throws Exception {

		if (newBeans == null || !(newBeans instanceof Admin)) {
			throw new BusinessException(
					"les nouvelle donne ne sont pas conforme");
		}

		this.setAssociedBean(dao.updateAdmin((Admin) newBeans));

	}
}
